<template>

  <el-form :inline="true" :model="formData" class="demo-form-inline">
    <el-form-item>
      <el-input v-model="formData.id" placeholder="请输入关键字" clearable @change="callApi" />
    </el-form-item>

    <el-form-item>
      <el-button type="primary" @click="onSubmit">查询</el-button>
    </el-form-item>
  </el-form>
  <el-table :data="tableData" style="width: 100%">
    <el-table-column fixed prop="id" label="id" width="200" />
    <el-table-column prop="vendorName" label="Name" width="120" />
    <el-table-column prop="ioDate" label="日期" width="120" />
    <el-table-column prop="receiver" label="入库人" width="120" />
    <el-table-column prop="brief" label="备注" width="200" show-overflow-tooltip />
    <el-table-column prop="lastUpdateBy" label="更新人" width="120" :formatter="formatter" />
    <el-table-column prop="lastUpdateTime" label="更新时间" width="120" :formatter="formatter" />
    <el-table-column fixed="right" label="Operations" min-width="140">
      <template #default="scope">
        <el-button type="primary" size="small" @click="edit(scope.row)">查看</el-button>
      </template>
    </el-table-column>
  </el-table>
  <el-pagination background layout="total, prev, pager, next" :total="pageData.total" :page-size="pageData.pageSize"
    @current-change="pageChange">
  </el-pagination>
  <el-drawer v-model="drawer" title="明细" :with-header="false" size="70%">
    <el-table :data="tableData2" style="width: 100%">
      <el-table-column fixed prop="productId" label="编号" width="60"></el-table-column>
      <el-table-column fixed prop="productName" label="名称" width="120"></el-table-column>
      <el-table-column label="入库数量" width="150" prop="qty"></el-table-column>
      <el-table-column label="生产日期" width="150" prop="productionDate"></el-table-column>
      <el-table-column label="保质期" width="100" prop="shelfLife"></el-table-column>
      <el-table-column label="入库货架" width="200" >
        <template #default="scope" >
          <ShelfTree v-model="scope.row.shelfId" style="width: 100px;" disabled="true"></ShelfTree>
        </template>
      </el-table-column>
      <el-table-column label="备注" width="300" prop="brief"></el-table-column>
    </el-table>
  </el-drawer>
</template>

<script lang="ts" setup>
import { ref, reactive, onMounted } from 'vue'
import { ioApi } from '@/api';
import { dayjs, ElMessageBox, ElMessage, rowContextKey } from 'element-plus'
import { useRouter } from 'vue-router'
import ShelfTree from '@/components/ShelfTree.vue'
const formData = reactive({
  id: '',
})
const drawer = ref(false)
const pageData = reactive({
  total: 0,
  pageSize: 5,
  pageNum: 1

})
const router = useRouter()
const tableData = ref([])
const tableData2 = ref([])
const edit = (row: any) => {
  ioApi.selectItems.call({ ioId: row.id }).then(res => {
    tableData2.value = res
    drawer.value = true
  })
}
const pageChange = (pageNum: number) => {
  pageData.pageNum = pageNum
  callApi()
}

const callApi = () => {
  let params = {
    pageNum: pageData.pageNum,
    pageSize: pageData.pageSize,
    id: formData.id == "" ? undefined : formData.id,

  }
  ioApi.select.call(params).then(res => {

    pageData.total = res.count
    tableData.value = res.items
    console.log(res)
  })
}

const formatter = (row: any, column: any, cellValue: any, index: number) => {

  if (column.property === 'lastUpdateTime') {
    return dayjs(cellValue).format('YY年MM月DD HH:mm')
  } else if (column.property === 'lastUpdateBy') {
    return cellValue + " 同志"
  }
  return cellValue
}

onMounted(() => {
  callApi()

})

const onSubmit = () => {
  callApi()
}
</script>